//
//  LSTabBar.swift
//  新浪微博
//
//  Created by 梁森 on 16/6/25.
//  Copyright © 2016年 梁森. All rights reserved.
//

import UIKit
protocol LSDelegate:NSObjectProtocol{
    func composehahaClick()
}
class LSTabBar: UITabBar {
    weak var lsDelegate:LSDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let btnW = UIScreen.mainScreen().bounds.size.width / 5
        
        var index:CGFloat = 0
        
        for value in self.subviews{
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                value.frame.size.width = btnW
                value.frame.origin.x = index * btnW
                index++
                if index == 2 {
                    index++
                }
            }
            composeButton.center.x = self.frame.size.width * 0.5
            composeButton.center.y = self.frame.size.height * 0.5
        }
    }
    
    
    func setupUI(){
        addSubview(composeButton)
    }
    
    @objc private func addClick() {
       lsDelegate?.composehahaClick()
    }
    
    private lazy var composeButton:UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "addClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        return button
        }()
}
